Adding ICPC Live Archive
[andmenj-acm.git] / ICPC Live Archive / 3928 - Ballroom lights / help / gomox.ar-pap-2010-d9b17e5cb110 / tpb / doc / trains.tex
bloba8543a5d850dc6a12d9f6f142f6a17d2b149f9bc
1 \section{10362 - Trains}
2 \textbf{Problema:} Dado un conjunto de rutas de tren, determinar las conexiones
3 ``\'optimas'' entre dos estaciones dadas. Una conexi\'on $c$ es \'optima
4 si se cumple lo siguiente:
6 \begin{enumerate}
7 \item[(A)] No hay una conexi\'on $c'$ que salga m\'as tarde y llegue m\'as temprano o a la misma hora que $c$.
8 \item[(B)] No hay una conexi\'on $c'$ que salga a la misma hora y llegue m\'as temprano que $c$.
9 \end{enumerate}
11 \subsection{Resolución}
13 La idea del algoritmo propuesto es primero encontrar los caminos m\'as
14 cortos (en minutos) entre la estaci\'on de origen y la estaci\'on de
15 destino. Estos cumplen con la condici\'on (B). Una vez hecho esto,
16 determinar cu\'ales de ellos cumplen con la condici\'on (A).
18 El problema de encontrar los caminos m\'as cortos se modela como un
19 problema de camino m\'inimo.
21 \subsubsection{Descripci\'on del grafo asociado al problema}
22 En primer lugar, construimos un grafo orientado con pesos en los ejes
23 $G = (V,E)$, cuyos nodos
24 representan una estaci\'on y una hora del d\'ia, y cuyos ejes representan
25 una manera de llegar de una pareja estaci\'on/hora a otra. El grafo
26 contiene los siguientes nodos:
28 $(n,h)\in V \longleftrightarrow$ existe un tren que pasa por la estaci\'on
29 $n$ a las $h$ horas $(h \in [\text{0:00}..\text{23:59}])$
31 %%$FINAL \in V$
33 Adem\'as, hay un eje desde $(n_1,h_1)$ hasta $(n_2,h_2) \longleftrightarrow$ se cumple alguno de los dos casos siguientes:
34 \begin{enumerate}
35 \item Existe un tren que pasa por $n_1$ a las $h_1$ y cuya siguiente parada es en $n_2$ a las $h_2$ (sin pasar por estaciones intermedias).
36 \item $n_1 = n_2$ (es la misma estaci\'on) y $h_2$ es el horario que sigue a $h_1$ en orden cronol\'ogico, sin pasar por horas intermedias.
37 \end{enumerate}
38 %\begin{enumerate}
39 %\item existe una ruta que permite tomar un tren en $n_1$ a las $h_1$ y llegar a $n_2$ a las $h_2$.
40 %\item $n_1=n_2 \wedge h_1<h_2 \wedge \nexists h_3 / (n_1,h_3) \in V \wedge h_1 < h_3 < h_2$
41 %\item $n_1=n_2 \wedge h_1 > h_2 \wedge \nexists h_3 / (n_1,h_3) \in V \wedge ( h_1 < h_3 \vee h_3 < h_2)$
42 %\end{enumerate}
44 Los ejes que resultan de la primera condici\'on tienen como peso asociado el tiempo de viaje.
45 Si bien se conoce la hora del d\'ia asociada a cada nodo, es necesario conocer el peso
46 expl\'icitamente, porque el tiempo de viaje entre dos estaciones {\em puede} ser mayor a un d\'ia.
48 Los ejes que resultan de la segunda condici\'on representan la posibilidad de quedarse
49 en una estaci\'on y esperar el próximo tren. En este caso, el peso del eje es la diferencia
50 entre $h_2$ y $h_1$. Cuando decimos que $h_2$ es el horario que sigue a $h_1$
51 ``en orden cronol\'ogico'', tambi\'en hay que considerar que $h_1$ puede ser
52 el \'ultimo tren del d\'ia y $h_2$ el primer tren del d\'ia siguiente. En ese caso
53 el peso del eje es la diferencia m\'odulo 24 horas.
55 %TODO: esto hay que explicarlo mas y/o mejor?
56 El grafo descripto modela el problema porque una conexión permite
57 llegar de una estaci\'on/hora a otra si y s\'olo si existe
58 un camino entre los dos nodos correspondientes del grafo, de manera tal que el tiempo que
59 tarda la conexi\'on es el peso del camino. Esto es porque o bien el
60 camino es una ruta directa sin cambiar de tren (en cuyo caso, por
61 los ejes del caso $1$, hay un camino en el grafo con el peso apropiado),
62 o bien la conexión usa fragmentos de varias rutas. Para hacer esto,
63 es necesario esperar cierto tiempo en una estaci\'on para cambiar de tren.
64 El tiempo de espera debe ser contabilizado
65 también, lo que se logra con los ejes del caso $2$. Si es posible hacer
66 una combinación sin esperar, es porque se llega a una estaci\'on a la misma
67 hora en que sale el siguiente tren. Esto no representa un problema
68 porque el enunciado indica que el tiempo necesario para realizar la conexión
69 está incorporado en los horarios de los trenes. Por el caso $1$, ese nodo
70 tiene ambos ejes, y el camino es realizable. De manera similar
71 se ve lo inverso, es decir que cualquier camino en el grafo representa
72 una conexi\'on v\'alida.
74 Notar que, en el primer caso, se agrega un eje s\'olo cuando la ruta no pasa por estaciones
75 intermedias. De manera similar, en el segundo caso, se agrega un eje de $(n,h_1)$ a $(n,h_2)$
76 s\'olo cuando no existe un horario $h_3$ entre $h_1$ y $h_2$.
77 Es decir, el grafo se construye {\em sin} hacer la clausura transitiva
78 de los ejes. No es necesario agregar los ejes restantes, porque lo que
79 importa es la duraci\'on en minutos de los caminos, y todos
80 los caminos posibles est\'an dados precisamente la clausura transitiva
81 de los ejes.
82 La ventaja es que de esta manera la cantidad de ejes del grafo
83 es lineal en el tama\~no de la entrada.
85 \subsubsection{B\'usqueda de los caminos m\'inimos}
87 Una vez que se tiene el grafo descripto en la secci\'on anterior,
88 el objetivo es determinar las duraciones de las conexiones \'optimas.
89 Las conexiones \'optimas deben ser caminos mínimos entre nodos de la estaci\'on de origen
90 ($src$) y la estaci\'on de destino ($dst$). Si no se tratara de caminos mínimos, sería posible
91 encontrar un camino que sale a la misma hora y llega antes (de menor duraci\'on, y por
92 lo tanto de menor peso), y la conexión no sería \'optima por no cumplir con
93 la condici\'on (B).
95 El primer objetivo es determinar, para cada nodo $(src,h)$ el peso del
96 camino m\'inimo hasta alg\'un nodo $(dst,h')$.
97 Los pesos de los ejes son todos positivos, por lo cual es relativamente
98 sencillo convertir este problema en un problema de camino m\'inimo
99 {\em single-source}, para as\'i poder aplicar el algoritmo de Dijkstra.
101 Para convertir el problema, se agrega un nodo $\textsc{final}$ y, para
102 cada nodo de la forma $(dst,h)$, un eje de peso 0 hacia el nodo $\textsc{final}$.
103 Por \'ultimo, se considera el grafo $G'$ que resulta de invertir
104 todos los ejes en $G$ y se calculan los caminos m\'inimos en $G'$ desde
105 $\textsc{final}$ hasta todos los dem\'as nodos, usando el algoritmo
106 de Dijkstra.
108 Cada uno de los caminos m\'inimos encontrados en $G'$ que terminan en nodos
109 de la forma $(src,h)$, corresponden a alg\'un camino en $G$ que empieza en $(src,h)$,
110 llega hasta alg\'un nodo $(dst,h')$, y por \'ultimo llega a $\textsc{final}$
111 mediante un eje de peso 0. El peso del camino correspondiente en $G$ es m\'inimo
112 y es el mismo que el encontrado en $G'$.
114 %Por otro lado, todas los nodos cuya estaci\'on es la de destino, tienen un eje de costo $0$ con el nodo $FINAL$.
116 Como observaci\'on adicional, dado que lo que se busca son caminos
117 m\'inimos, si una estaci\'on $n$ tiene un \'unico horario $h$ en el que salen o
118 llegan trenes, durante la construcci\'on del grafo $G$ no se agrega el autoeje
119 $(n,h)$, porque este eje tiene peso de 24 horas y obviamente nunca es conveniente
120 (existe un camino desde $v$ hasta $w$ que pasa por dicho eje si y s\'olo si existe
121 un camino de $v$ a $w$ de menor peso que no lo utiliza).
123 \begin{figure}[H]
124 \centering
125 \includegraphics[scale=0.3]{./figuras/grafo.pdf}
126 \caption{Grafo resultante para la entrada de ejemplo (antes de invertir los ejes)}
127 \end{figure}
129 \subsubsection{Determinaci\'on de las conexiones \'optimas}
131 Una vez obtenidas las duraciones m\'inimas de los caminos que parten de cada
132 uno de los nodos $(src,h)$, resta s\'olo filtrar de esos caminos aquellos que
133 cumplen con la condici\'on (A).
135 Notar que la condici\'on (A) usa la expresi\'on ``llegar m\'as temprano'' en
136 el sentido que uno le atribuye intuitivamente, y no a la hora absoluta del día.
137 Por ejemplo, salir a las 21:00 y llegar a las 22:00 es llegar m\'as temprano
138 que salir a las 20:00 y llegar a la 1:00, pese a que $\text{1:00} < \text{22:00}$.
139 Esto es obvio, pero indica que la implementaci\'on debe comparar las duraciones
140 totales de los viajes m\'as que las horas de llegada.
141 Algo similar aplica para la expresi\'on ``salir m\'as tarde''.
142 %En la resolución del problema consideramos el tiempo como cantidad de minutos.
144 El siguiente es el algoritmo para elegir las rutas, asumiendo que
145 las distancias desde cada nodo $v$ hasta el nodo $\textsc{final}$
146 ya fueron calculadas:
148 \begin{algorithm}[H]
149 \begin{algorithmic}
150 \caption{Determina las conexiones \'optimas}
151 \PARAMS{$distancia_v$ = duraci\'on mínima de la conexi\'on desde un nodo $v$ hasta el nodo $\textsc{final}$}
152 \STATE mínima hora de llegada $:= \infty$
153 \FOR{cada nodo $v$ de la forma $(src,h)$}
154 \STATE hora de llegada $:= h + distancia_{v} + $ 24 hs
155 \STATE\COMMENT{tiempo del nodo es la cantidad de tiempo hasta que sale el tren (i.e: si el tren sale a las 8, es la cantidad de minutos hasta las 8)}
156 \STATE mínima hora de llegada $:= \min($hora de llegada, mínima hora de llegada$)$
157 \ENDFOR
158 \FOR{cada nodo $v$ de la forma $(src,h)$ (se recorren por hora decreciente)}
159 %\STATE hora de salida $:= h$
160 \STATE hora de llegada $:= h + distancia_{v}$
161 \IF{hora de llegada $<$ mínima hora de llegada}
162 \STATE mínima hora de llegada $:=$ hora de llegada
163 \STATE reportar la conexi\'on que sale a la hora $h$ y tarda $distancia_{v}$ en llegar
164 \ENDIF
165 \ENDFOR
166 \end{algorithmic}
167 \end{algorithm}
169 El primer {\bf for} inicializa el valor de la variable ``m\'inima hora de llegada''.
170 Para esto se busca, entre todos los nodos $(src,h)$, el que minimiza $h + distancia_{(src,h)} + \text{24 hs}$.
171 Este valor $H$ representa la hora m\'as temprana a la que se puede llegar a destino partiendo al
172 d\'ia siguiente, y se usa como cota superior para descartar algunas de las conexiones
173 que no son \'optimas.
174 Es decir, si $distancia_{(src,h)} \geq H$, la conexi\'on m\'as corta que empieza
175 en la estaci\'on de origen tomando el tren de la hora $h$ llega a lo sumo tan temprano
176 como alguna otra conexi\'on posterior, y por lo tanto no es \'optima.
178 Notar que alcanza con usar como cota superior la hora de llegada de
179 la conexi\'on m\'as corta que empieza en el {\em segundo} d\'ia
180 (y no es necesario mirar m\'as d\'ias hacia adelante) porque
181 la conexi\'on m\'as corta que empieza en el cualquier d\'ia
182 posterior al segundo llega despu\'es de $H$.
184 En el segundo {\bf for}, se tiene como invariante que la variable ``m\'inima hora de llegada''
185 es la m\'inima hora a la que se puede llegar con una conexi\'on que sale en
186 una hora posterior a $h$. Inicialmente esto es cierto porque, como ya se justific\'o,
187 ``m\'inima hora de llegada'' se inicializa en $H$. El invariante se mantiene porque
188 los nodos se recorren por hora decreciente. En cada paso, si la hora a la que se
189 llega con una conexi\'on que empieza desde el nodo $v$ es menor que la m\'inima
190 hora a la que se puede llegar saliendo despu\'es de la hora $h$, esto quiere
191 decir que dicha conexi\'on cumple con la condici\'on (A). Adem\'as, es \'optima
192 porque tambi\'en cumpl\'ia con la condici\'on (B).
194 \subsubsection{Detalles de implementaci\'on y complejidad}
196 Todas las horas se representan en minutos. El grafo se representa con listas
197 de adyacencia. Cada nodo se identifica mediante un par $\langle id, hora \rangle$,
198 donde $id$ es un entero que representa una estaci\'on. Las listas de adyacencia se guardan
199 en un diccionario de nodo a lista de vecinos, cada uno con el peso del eje
200 correspondiente.
202 Llamamos $N$ a la cantidad de estaciones y $M$ a la cantidad de ejes
203 del grafo asociado al problema. Notar que $N \in O(M)$ porque no puede
204 haber nodos aislados, ya que si una estaci\'on
205 ocurre en la entrada, debe figurar en al menos una ruta. Adem\'as,
206 $M$ es lineal en el tama\~no de la entrada, porque
207 los ejes que representan el camino de un tren deben estar
208 expl\'icitos en la entrada, mientras la cantidad de ejes que
209 conectan nodos de la misma estaci\'on es (por la manera en la
210 que se construye el grafo) a lo sumo igual a la cantidad total
211 de nodos de la estaci\'on.
212 % Para construir el grafo, la informaci\'on de las estaciones
213 %se guarda en un diccionario que dado el nombre de la estaci\'on devuelve su
214 %id y el conjunto de horas a las que un tren pasa por dicha estaci\'on.
216 La implementaci\'on del algoritmo de Dijkstra usa una cola de prioridad
217 representada mediante un conjunto de tuplas $\langle distancia, nodo \rangle$,
218 por lo que la complejidad del algoritmo es $O(M \log N)$. El costo del
219 algoritmo que determina las conexiones \'optimas es $O(N \log N)$.
220 Previo a resolver el problema, el costo de procesar la entrada es el siguiente:
221 \begin{itemize}
222 \item Primero se asigna un id a cada estaci\'on, y se construye un
223 diccionario que permite ver, dada una estaci\'on (\texttt{string}), qu\'e
224 id tiene y a qué horas hay trenes que llegan o salen. El costo de
225 esto es $O(N \log N)$ asumiendo, de acuerdo con el enunciado del
226 problema, que los nombres de las estaciones tienen un largo m\'aximo fijo
227 (y razonablemente chico).
228 \item Despu\'es se construyen las listas de adyacencia.
229 Esto toma $O(M \log N + N \log N)$. El segundo término proviene del costo de agregar
230 los ejes entre nodos de la misma estación. Esto se puede hacer en $O(N \log N)$ iterando las
231 claves del diccionario que guarda los ids y las horas de cada estaci\'on.
232 \end{itemize}
234 Finalmente la complejidad es $O(N \log N + M \log N) \subseteq O(M \log N)$.
236 \subsection{Implementación}
237 \noindent
238 \ttfamily
239 \shorthandoff{"}\\
240 \hlstd{}\hlline{\ 1\ }\hldir{\#include\ $<$iostream$>$}\\
241 \hlline{\ 2\ }\hlstd{}\hldir{\#include\ $<$string$>$}\\
242 \hlline{\ 3\ }\hlstd{}\hldir{\#include\ $<$map$>$}\\
243 \hlline{\ 4\ }\hlstd{}\hldir{\#include\ $<$set$>$}\\
244 \hlline{\ 5\ }\hlstd{}\hldir{\#include\ $<$vector$>$}\\
245 \hlline{\ 6\ }\hlstd{}\hldir{\#include\ $<$list$>$}\\
246 \hlline{\ 7\ }\hlstd{}\hldir{\#include\ $<$cassert$>$}\\
247 \hlline{\ 8\ }\hlstd{}\hldir{\#include\ $<$cstdlib$>$}\\
248 \hlline{\ 9\ }\hlstd{}\\
249 \hlline{10\ }\hlkwa{using\ namespace\ }\hlstd{std}\hlsym{;}\\
250 \hlline{11\ }\hlstd{}\\
251 \hlline{12\ }\hldir{\#define\ forsn(i,\ s,\ n)\ for\ (int\ i\ =\ (s);\ i\ $<$\ (n);\ i++)}\\
252 \hlline{13\ }\hlstd{}\hldir{\#define\ forn(i,\ n)}\hlstd{\ \ }\hldir{forsn\ (i,\ 0,\ n)}\\
253 \hlline{14\ }\hlstd{}\hldir{\#define\ forall(x,\ s)\ for\ (typeof((s).begin())\ x\ =\ (s).begin();\ x\ !=\ (s).end();\ x++)}\\
254 \hlline{15\ }\hlstd{}\hldir{\#define\ dforall(x,\ s)\ for\ (typeof((s).rbegin())\ x\ =\ (s).rbegin();\ x\ !=\ (s).rend();\ x++)}\\
255 \hlline{16\ }\hlstd{}\\
256 \hlline{17\ }\hlkwc{typedef\ }\hlstd{}\hlkwb{int\ }\hlstd{Id}\hlsym{;\ }\hlstd{}\hlslc{//\ station\ id}\\
257 \hlline{18\ }\hlstd{}\hlkwc{typedef\ }\hlstd{}\hlkwb{int\ }\hlstd{Time}\hlsym{;}\\
258 \hlline{19\ }\hlstd{}\hlkwc{typedef\ }\hlstd{pair}\hlsym{$<$}\hlstd{Id}\hlsym{,\ }\hlstd{Time}\hlsym{$>$\ }\hlstd{Nodo}\hlsym{;}\\
259 \hlline{20\ }\hlstd{}\\
260 \hlline{21\ }\hlkwb{struct\ }\hlstd{Edge\ }\hlsym{\{}\\
261 \hlline{22\ }\hlstd{\ Nodo\ dest}\hlsym{;}\\
262 \hlline{23\ }\hlstd{\ Time\ cost}\hlsym{;}\\
263 \hlline{24\ }\hlstd{}\hlsym{\};}\\
264 \hlline{25\ }\hlstd{}\hlkwc{typedef\ }\hlstd{vector}\hlsym{$<$}\hlstd{Edge}\hlsym{$>$\ }\hlstd{Ady}\hlsym{;}\\
265 \hlline{26\ }\hlstd{}\hlkwc{typedef\ }\hlstd{map}\hlsym{$<$}\hlstd{Nodo}\hlsym{,\ }\hlstd{Ady}\hlsym{$>$\ }\hlstd{Grafo}\hlsym{;}\\
266 \hlline{27\ }\hlstd{}\\
267 \hlline{28\ }\hlkwc{typedef\ }\hlstd{map}\hlsym{$<$}\hlstd{string}\hlsym{,\ }\hlstd{pair}\hlsym{$<$}\hlstd{Id}\hlsym{,\ }\hlstd{set}\hlsym{$<$}\hlstd{Time}\hlsym{$>$\ $>$\ $>$\ }\hlstd{Stations}\hlsym{;}\\
268 \hlline{29\ }\hlstd{}\\
269 \hlline{30\ }\hlslc{//\ map$<$Nodo,\ Ady$>$::iterator}\\
270 \hlline{31\ }\hlstd{}\hldir{\#define\ \textunderscore nodo}\hlstd{\ \ }\hldir{first}\\
271 \hlline{32\ }\hlstd{}\hldir{\#define\ \textunderscore ady}\hlstd{\ \ }\hldir{second}\\
272 \hlline{33\ }\hlstd{}\\
273 \hlline{34\ }\hlslc{//\ Stations::iterator}\\
274 \hlline{35\ }\hlstd{}\hldir{\#define\ \textunderscore info}\hlstd{\ \ }\hldir{second}\\
275 \hlline{36\ }\hlstd{}\\
276 \hlline{37\ }\hlslc{//\ pair$<$Id,\ set$<$Time$>$\ $>$}\\
277 \hlline{38\ }\hlstd{}\hldir{\#define\ \textunderscore id}\hlstd{\ \ }\hldir{first}\\
278 \hlline{39\ }\hlstd{}\hldir{\#define\ \textunderscore timeset\ second}\\
279 \hlline{40\ }\hlstd{}\\
280 \hlline{41\ }\hlslc{//\ Nodo}\\
281 \hlline{42\ }\hlstd{}\hldir{\#define\ \textunderscore time}\hlstd{\ \ }\hldir{second}\\
282 \hlline{43\ }\hlstd{}\\
283 \hlline{44\ }\hldir{\#define\ HOUR\ 60}\\
284 \hlline{45\ }\hlstd{}\hldir{\#define\ DAY\ (24\ {*}\ HOUR)}\\
285 \hlline{46\ }\hlstd{\\
286 \hlline{47\ }Id\ }\hlkwc{inline\ }\hlstd{}\hlkwd{register\textunderscore station}\hlstd{}\hlsym{(}\hlstd{Stations}\hlsym{\&\ }\hlstd{stations}\hlsym{,\ }\hlstd{}\hlkwb{const\ }\hlstd{string}\hlsym{\&\ }\hlstd{station\textunderscore name}\hlsym{,\ }\hlstd{Time\ time}\hlsym{)\ \{}\\
287 \hlline{48\ }\hlstd{\ Stations}\hlsym{::}\hlstd{iterator\ }\hlkwd{it}\hlstd{}\hlsym{(}\hlstd{stations}\hlsym{.}\hlstd{}\hlkwd{find}\hlstd{}\hlsym{(}\hlstd{station\textunderscore name}\hlsym{));}\\
288 \hlline{49\ }\hlstd{\ }\hlkwa{if\ }\hlstd{}\hlsym{(}\hlstd{it\ }\hlsym{==\ }\hlstd{stations}\hlsym{.}\hlstd{}\hlkwd{end}\hlstd{}\hlsym{())\ \{}\\
289 \hlline{50\ }\hlstd{}\hlstd{\ \ }\hlstd{}\hlslc{//\ create}\\
290 \hlline{51\ }\hlstd{}\hlstd{\ \ }\hlstd{}\hlkwb{const\ }\hlstd{Id\ a\ }\hlsym{=\ }\hlstd{stations}\hlsym{.}\hlstd{}\hlkwd{size}\hlstd{}\hlsym{();}\\
291 \hlline{52\ }\hlstd{}\hlstd{\ \ }\hlstd{set}\hlsym{$<$}\hlstd{Time}\hlsym{$>$\ }\hlstd{s}\hlsym{;}\\
292 \hlline{53\ }\hlstd{}\hlstd{\ \ }\hlstd{s}\hlsym{.}\hlstd{}\hlkwd{insert}\hlstd{}\hlsym{(}\hlstd{time}\hlsym{);}\\
293 \hlline{54\ }\hlstd{}\hlstd{\ \ }\hlstd{stations}\hlsym{{[}}\hlstd{station\textunderscore name}\hlsym{{]}\ =\ }\hlstd{pair}\hlsym{$<$}\hlstd{Id}\hlsym{,\ }\hlstd{set}\hlsym{$<$}\hlstd{Time}\hlsym{$>$\ $>$(}\hlstd{a}\hlsym{,\ }\hlstd{s}\hlsym{);}\\
294 \hlline{55\ }\hlstd{}\hlstd{\ \ }\hlstd{}\hlkwa{return\ }\hlstd{a}\hlsym{;}\\
295 \hlline{56\ }\hlstd{\ }\hlsym{\}\ }\hlstd{}\hlkwa{else\ }\hlstd{}\hlsym{\{}\\
296 \hlline{57\ }\hlstd{}\hlstd{\ \ }\hlstd{pair}\hlsym{$<$}\hlstd{Id}\hlsym{,}\hlstd{set}\hlsym{$<$}\hlstd{Time}\hlsym{$>$\ $>$\&\ }\hlstd{t\ }\hlsym{=\ }\hlstd{stations}\hlsym{{[}}\hlstd{station\textunderscore name}\hlsym{{]};}\\
297 \hlline{58\ }\hlstd{}\hlstd{\ \ }\hlstd{t}\hlsym{.}\hlstd{\textunderscore timeset}\hlsym{.}\hlstd{}\hlkwd{insert}\hlstd{}\hlsym{(}\hlstd{time}\hlsym{);}\\
298 \hlline{59\ }\hlstd{}\hlstd{\ \ }\hlstd{}\hlkwa{return\ }\hlstd{t}\hlsym{.}\hlstd{\textunderscore id}\hlsym{;}\\
299 \hlline{60\ }\hlstd{\ }\hlsym{\}}\\
300 \hlline{61\ }\hlstd{}\hlsym{\}}\\
301 \hlline{62\ }\hlstd{}\\
302 \hlline{63\ }\hlkwc{inline\ }\hlstd{}\hlkwb{int\ }\hlstd{}\hlkwd{time\textunderscore in\textunderscore minutes}\hlstd{}\hlsym{(}\hlstd{string}\hlsym{\&\ }\hlstd{s}\hlsym{)\ \{}\\
303 \hlline{64\ }\hlstd{\ }\hlslc{//\ h:mm\ hh:mm\ hhh:mm\ ...}\\
304 \hlline{65\ }\hlstd{\ }\hlkwb{const\ int\ }\hlstd{l\ }\hlsym{=\ }\hlstd{s}\hlsym{.}\hlstd{}\hlkwd{size}\hlstd{}\hlsym{();}\\
305 \hlline{66\ }\hlstd{\ }\hlkwd{assert}\hlstd{}\hlsym{(}\hlstd{s}\hlsym{{[}}\hlstd{l\ }\hlsym{{-}\ }\hlstd{}\hlnum{3}\hlstd{}\hlsym{{]}\ ==\ }\hlstd{}\hlstr{`:`}\hlstd{}\hlsym{);}\\
306 \hlline{67\ }\hlstd{\ }\hlkwa{return\ }\hlstd{}\hlkwd{atoi}\hlstd{}\hlsym{(}\hlstd{s}\hlsym{.}\hlstd{}\hlkwd{substr}\hlstd{}\hlsym{(}\hlstd{}\hlnum{0}\hlstd{}\hlsym{,\ }\hlstd{l\ }\hlsym{{-}\ }\hlstd{}\hlnum{3}\hlstd{}\hlsym{).}\hlstd{}\hlkwd{c\textunderscore str}\hlstd{}\hlsym{())\ {*}\ }\hlstd{HOUR\ }\hlsym{+\ }\hlstd{}\hlkwd{atoi}\hlstd{}\hlsym{(}\hlstd{s}\hlsym{.}\hlstd{}\hlkwd{substr}\hlstd{}\hlsym{(}\hlstd{l\ }\hlsym{{-}\ }\hlstd{}\hlnum{2}\hlstd{}\hlsym{,\ }\hlstd{}\hlnum{2}\hlstd{}\hlsym{).}\hlstd{}\hlkwd{c\textunderscore str}\hlstd{}\hlsym{());}\\
307 \hlline{68\ }\hlstd{}\hlsym{\}}\\
308 \hlline{69\ }\hlstd{}\\
309 \hlline{70\ }\hlkwb{void\ }\hlstd{}\hlkwd{print\textunderscore graph}\hlstd{}\hlsym{(}\hlstd{Grafo}\hlsym{\&\ }\hlstd{g}\hlsym{)\ \{}\\
310 \hlline{71\ }\hlstd{\ }\hlkwd{forall\ }\hlstd{}\hlsym{(}\hlstd{x}\hlsym{,\ }\hlstd{g}\hlsym{)\ \{}\\
311 \hlline{72\ }\hlstd{}\hlstd{\ \ }\hlstd{cout\ }\hlsym{$<$$<$\ }\hlstd{}\hlstr{"Hasta:\ "}\hlstd{\ }\hlsym{$<$$<$\ }\hlstd{x}\hlsym{{-}$>$}\hlstd{\textunderscore nodo}\hlsym{.}\hlstd{\textunderscore id\ }\hlsym{$<$$<$\ }\hlstd{}\hlstr{"\ a\ las\ "}\hlstd{\ }\hlsym{$<$$<$\ (}\hlstd{x}\hlsym{{-}$>$}\hlstd{\textunderscore nodo}\hlsym{.}\hlstd{\textunderscore time\ }\hlsym{/\ }\hlstd{}\hlnum{60}\hlstd{}\hlsym{)\ $<$$<$\ }\hlstd{}\hlstr{":"}\hlstd{\ }\hlsym{$<$$<$\ (}\hlstd{x}\hlsym{{-}$>$}\hlstd{\textunderscore nodo}\hlsym{.}\hlstd{\textunderscore time\ \Righttorque\\
312 \hlline{73\ }}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{}\hlsym{\%\ }\hlstd{}\hlnum{60}\hlstd{}\hlsym{)\ $<$$<$\ }\hlstd{endl}\hlsym{;}\\
313 \hlline{74\ }\hlstd{}\hlstd{\ \ }\hlstd{}\hlkwd{forall\ }\hlstd{}\hlsym{(}\hlstd{y}\hlsym{,\ }\hlstd{x}\hlsym{{-}$>$}\hlstd{\textunderscore ady}\hlsym{)\ \{}\\
314 \hlline{75\ }\hlstd{}\hlstd{\ \ \ }\hlstd{cout\ }\hlsym{$<$$<$\ }\hlstd{}\hlstr{"}\hlstd{\ \ \ \ }\hlstr{desde\ "}\hlstd{\ }\hlsym{$<$$<$\ }\hlstd{y}\hlsym{{-}$>$}\hlstd{dest}\hlsym{.}\hlstd{\textunderscore id\\
315 \hlline{76\ }}\hlstd{\ \ \ \ }\hlstd{}\hlsym{$<$$<$\ }\hlstd{}\hlstr{"\ a\ las\ "}\hlstd{\ }\hlsym{$<$$<$\ (}\hlstd{y}\hlsym{{-}$>$}\hlstd{dest}\hlsym{.}\hlstd{\textunderscore time\ }\hlsym{/\ }\hlstd{}\hlnum{60}\hlstd{}\hlsym{)\ $<$$<$\ }\hlstd{}\hlstr{":"}\hlstd{\ }\hlsym{$<$$<$\ (}\hlstd{y}\hlsym{{-}$>$}\hlstd{dest}\hlsym{.}\hlstd{\textunderscore time\ }\hlsym{\%\ }\hlstd{}\hlnum{60}\hlstd{}\hlsym{)}\\
316 \hlline{77\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlsym{$<$$<$\ }\hlstd{}\hlstr{"\ costo:\ "}\hlstd{\ }\hlsym{$<$$<$\ (}\hlstd{y}\hlsym{{-}$>$}\hlstd{cost\ }\hlsym{/\ }\hlstd{}\hlnum{60}\hlstd{}\hlsym{)\ $<$$<$\ }\hlstd{}\hlstr{":"}\hlstd{\ }\hlsym{$<$$<$\ (}\hlstd{y}\hlsym{{-}$>$}\hlstd{cost\ }\hlsym{\%\ }\hlstd{}\hlnum{60}\hlstd{}\hlsym{)\ $<$$<$\ }\hlstd{endl}\hlsym{;}\\
317 \hlline{78\ }\hlstd{}\hlstd{\ \ }\hlstd{}\hlsym{\}}\\
318 \hlline{79\ }\hlstd{\ }\hlsym{\}}\\
319 \hlline{80\ }\hlstd{}\hlsym{\}}\\
320 \hlline{81\ }\hlstd{}\\
321 \hlline{82\ }\hldir{\#define\ INF\ 0x7fffffff}\\
322 \hlline{83\ }\hlstd{}\\
323 \hlline{84\ }\hlkwc{inline\ }\hlstd{}\hlkwb{void\ }\hlstd{}\hlkwd{dijkstra}\hlstd{}\hlsym{(}\hlstd{Stations}\hlsym{\&\ }\hlstd{stations}\hlsym{,\ }\hlstd{Grafo}\hlsym{\&\ }\hlstd{graph}\hlsym{,\ }\hlstd{}\hlkwb{const\ }\hlstd{string}\hlsym{\&\ }\hlstd{origen}\hlsym{,\ }\hlstd{}\hlkwb{const\ }\hlstd{string}\hlsym{\&\ }\hlstd{destino}\hlsym{)\ \{}\\
324 \hlline{85\ }\hlstd{\ }\hlkwb{const\ }\hlstd{Id\ id\textunderscore origen\ }\hlsym{=\ }\hlstd{stations}\hlsym{{[}}\hlstd{origen}\hlsym{{]}.}\hlstd{\textunderscore id}\hlsym{;}\\
325 \hlline{86\ }\hlstd{\ }\hlkwb{const\ }\hlstd{Id\ id\textunderscore destino\ }\hlsym{=\ }\hlstd{stations}\hlsym{{[}}\hlstd{destino}\hlsym{{]}.}\hlstd{\textunderscore id}\hlsym{;}\\
326 \hlline{87\ }\hlstd{\\
327 \hlline{88\ }\ set}\hlsym{$<$}\hlstd{pair}\hlsym{$<$}\hlstd{Time}\hlsym{,\ }\hlstd{Nodo}\hlsym{$>$\ $>$\ }\hlstd{cola}\hlsym{;}\\
328 \hlline{89\ }\hlstd{\ map}\hlsym{$<$}\hlstd{Nodo}\hlsym{,\ }\hlstd{Time}\hlsym{$>$\ }\hlstd{distancia}\hlsym{;}\\
329 \hlline{90\ }\hlstd{\\
330 \hlline{91\ }\ }\hlslc{//\ inicializar\ distancias}\\
331 \hlline{92\ }\hlstd{\ }\hlkwd{forall\ }\hlstd{}\hlsym{(}\hlstd{st}\hlsym{,\ }\hlstd{stations}\hlsym{)\ \{}\\
332 \hlline{93\ }\hlstd{}\hlstd{\ \ }\hlstd{Id\ station\textunderscore id\ }\hlsym{=\ }\hlstd{st}\hlsym{{-}$>$}\hlstd{\textunderscore info}\hlsym{.}\hlstd{\textunderscore id}\hlsym{;}\\
333 \hlline{94\ }\hlstd{}\hlstd{\ \ }\hlstd{}\hlkwd{forall\ }\hlstd{}\hlsym{(}\hlstd{t}\hlsym{,\ }\hlstd{st}\hlsym{{-}$>$}\hlstd{\textunderscore info}\hlsym{.}\hlstd{\textunderscore timeset}\hlsym{)\ \{}\\
334 \hlline{95\ }\hlstd{}\hlstd{\ \ \ }\hlstd{Nodo\ }\hlkwd{v}\hlstd{}\hlsym{(}\hlstd{station\textunderscore id}\hlsym{,\ {*}}\hlstd{t}\hlsym{);}\\
335 \hlline{96\ }\hlstd{}\hlstd{\ \ \ }\hlstd{}\hlkwa{if\ }\hlstd{}\hlsym{(}\hlstd{station\textunderscore id\ }\hlsym{==\ }\hlstd{id\textunderscore destino}\hlsym{)\ \{}\\
336 \hlline{97\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{cola}\hlsym{.}\hlstd{}\hlkwd{insert}\hlstd{}\hlsym{(}\hlstd{pair}\hlsym{$<$}\hlstd{Time}\hlsym{,}\hlstd{Nodo}\hlsym{$>$(}\hlstd{}\hlnum{0}\hlstd{}\hlsym{,\ }\hlstd{v}\hlsym{));}\\
337 \hlline{98\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{distancia}\hlsym{{[}}\hlstd{v}\hlsym{{]}\ =\ }\hlstd{}\hlnum{0}\hlstd{}\hlsym{;}\\
338 \hlline{99\ }\hlstd{}\hlstd{\ \ \ }\hlstd{}\hlsym{\}\ }\hlstd{}\hlkwa{else\ }\hlstd{}\hlsym{\{}\\
339 \hlline{100\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{distancia}\hlsym{{[}}\hlstd{v}\hlsym{{]}\ =\ }\hlstd{INF}\hlsym{;}\\
340 \hlline{101\ }\hlstd{}\hlstd{\ \ \ }\hlstd{}\hlsym{\}}\\
341 \hlline{102\ }\hlstd{}\hlstd{\ \ }\hlstd{}\hlsym{\}}\\
342 \hlline{103\ }\hlstd{\ }\hlsym{\}}\\
343 \hlline{104\ }\hlstd{\ }\hlkwa{while\ }\hlstd{}\hlsym{(!}\hlstd{cola}\hlsym{.}\hlstd{}\hlkwd{empty}\hlstd{}\hlsym{())\ \{}\\
344 \hlline{105\ }\hlstd{}\hlstd{\ \ }\hlstd{}\hlslc{//\ buscar\ el\ minimo}\\
345 \hlline{106\ }\hlstd{}\hlstd{\ \ }\hlstd{Time\ min\textunderscore dist\ }\hlsym{=\ }\hlstd{cola}\hlsym{.}\hlstd{}\hlkwd{begin}\hlstd{}\hlsym{(){-}$>$}\hlstd{first}\hlsym{;}\\
346 \hlline{107\ }\hlstd{\\
347 \hlline{108\ }}\hlstd{\ \ }\hlstd{}\hlslc{//\ borrarlo\ de\ la\ cola}\\
348 \hlline{109\ }\hlstd{}\hlstd{\ \ }\hlstd{Nodo\ min\textunderscore node\ }\hlsym{=\ }\hlstd{cola}\hlsym{.}\hlstd{}\hlkwd{begin}\hlstd{}\hlsym{(){-}$>$}\hlstd{second}\hlsym{;}\\
349 \hlline{110\ }\hlstd{}\hlstd{\ \ }\hlstd{cola}\hlsym{.}\hlstd{}\hlkwd{erase}\hlstd{}\hlsym{(}\hlstd{cola}\hlsym{.}\hlstd{}\hlkwd{begin}\hlstd{}\hlsym{());}\\
350 \hlline{111\ }\hlstd{\\
351 \hlline{112\ }}\hlstd{\ \ }\hlstd{}\hlslc{//\ procesar\ min\textunderscore node}\\
352 \hlline{113\ }\hlstd{}\hlstd{\ \ }\hlstd{}\hlkwd{forall\ }\hlstd{}\hlsym{(}\hlstd{eje}\hlsym{,\ }\hlstd{graph}\hlsym{{[}}\hlstd{min\textunderscore node}\hlsym{{]})\ \{}\\
353 \hlline{114\ }\hlstd{}\hlstd{\ \ \ }\hlstd{}\hlkwb{const\ }\hlstd{Time\ dist\textunderscore nueva\ }\hlsym{=\ }\hlstd{min\textunderscore dist\ }\hlsym{+\ }\hlstd{eje}\hlsym{{-}$>$}\hlstd{cost}\hlsym{;}\\
354 \hlline{115\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{}\hlkwb{const\ }\hlstd{Time\ dist\textunderscore actual\ }\hlsym{=\ }\hlstd{distancia}\hlsym{{[}}\hlstd{eje}\hlsym{{-}$>$}\hlstd{dest}\hlsym{{]};}\\
355 \hlline{116\ }\hlstd{}\hlstd{\ \ \ }\hlstd{}\hlkwa{if\ }\hlstd{}\hlsym{(}\hlstd{dist\textunderscore nueva\ }\hlsym{$<$\ }\hlstd{dist\textunderscore actual}\hlsym{)\ \{}\\
356 \hlline{117\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwa{if\ }\hlstd{}\hlsym{(}\hlstd{dist\textunderscore actual\ }\hlsym{!=\ }\hlstd{INF}\hlsym{)\ \{}\\
357 \hlline{118\ }\hlstd{}\hlstd{\ \ \ \ \ }\hlstd{cola}\hlsym{.}\hlstd{}\hlkwd{erase}\hlstd{}\hlsym{(}\hlstd{cola}\hlsym{.}\hlstd{}\hlkwd{find}\hlstd{}\hlsym{(}\hlstd{pair}\hlsym{$<$}\hlstd{Time}\hlsym{,}\hlstd{Nodo}\hlsym{$>$(}\hlstd{dist\textunderscore actual}\hlsym{,\ }\hlstd{eje}\hlsym{{-}$>$}\hlstd{dest}\hlsym{)));}\\
358 \hlline{119\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlsym{\}}\\
359 \hlline{120\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{distancia}\hlsym{{[}}\hlstd{eje}\hlsym{{-}$>$}\hlstd{dest}\hlsym{{]}\ =\ }\hlstd{dist\textunderscore nueva}\hlsym{;}\\
360 \hlline{121\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{cola}\hlsym{.}\hlstd{}\hlkwd{insert}\hlstd{}\hlsym{(}\hlstd{pair}\hlsym{$<$}\hlstd{Time}\hlsym{,}\hlstd{Nodo}\hlsym{$>$(}\hlstd{dist\textunderscore nueva}\hlsym{,\ }\hlstd{eje}\hlsym{{-}$>$}\hlstd{dest}\hlsym{));}\\
361 \hlline{122\ }\hlstd{}\hlstd{\ \ \ }\hlstd{}\hlsym{\}}\\
362 \hlline{123\ }\hlstd{}\hlstd{\ \ }\hlstd{}\hlsym{\}}\\
363 \hlline{124\ }\hlstd{\ }\hlsym{\}}\\
364 \hlline{125\ }\hlstd{\\
365 \hlline{126\ }\ list}\hlsym{$<$}\hlstd{pair}\hlsym{$<$}\hlstd{Time}\hlsym{,\ }\hlstd{Time}\hlsym{$>$\ $>$\ }\hlstd{salida}\hlsym{;}\\
366 \hlline{127\ }\hlstd{\ Time\ min\textunderscore hora\textunderscore llega\ }\hlsym{=\ }\hlstd{INF}\hlsym{;}\\
367 \hlline{128\ }\hlstd{\ }\hlkwd{forall\ }\hlstd{}\hlsym{(}\hlstd{t}\hlsym{,\ }\hlstd{stations}\hlsym{{[}}\hlstd{origen}\hlsym{{]}.}\hlstd{\textunderscore timeset}\hlsym{)\ \{}\\
368 \hlline{129\ }\hlstd{}\hlstd{\ \ }\hlstd{Nodo\ }\hlkwd{v}\hlstd{}\hlsym{(}\hlstd{id\textunderscore origen}\hlsym{,\ {*}}\hlstd{t}\hlsym{);}\\
369 \hlline{130\ }\hlstd{}\hlstd{\ \ }\hlstd{}\hlkwb{const\ }\hlstd{Time\ hora\textunderscore llega\ }\hlsym{=\ {*}}\hlstd{t\ }\hlsym{+\ }\hlstd{distancia}\hlsym{{[}}\hlstd{v}\hlsym{{]}\ +\ }\hlstd{DAY}\hlsym{;}\\
370 \hlline{131\ }\hlstd{}\hlstd{\ \ }\hlstd{min\textunderscore hora\textunderscore llega\ }\hlsym{=\ }\hlstd{}\hlkwd{min}\hlstd{}\hlsym{(}\hlstd{min\textunderscore hora\textunderscore llega}\hlsym{,\ }\hlstd{hora\textunderscore llega}\hlsym{);}\\
371 \hlline{132\ }\hlstd{\ }\hlsym{\}}\\
372 \hlline{133\ }\hlstd{\ }\hlkwd{dforall\ }\hlstd{}\hlsym{(}\hlstd{t}\hlsym{,\ }\hlstd{stations}\hlsym{{[}}\hlstd{origen}\hlsym{{]}.}\hlstd{\textunderscore timeset}\hlsym{)\ \{}\\
373 \hlline{134\ }\hlstd{}\hlstd{\ \ }\hlstd{Nodo\ }\hlkwd{v}\hlstd{}\hlsym{(}\hlstd{id\textunderscore origen}\hlsym{,\ {*}}\hlstd{t}\hlsym{);}\\
374 \hlline{135\ }\hlstd{}\hlstd{\ \ }\hlstd{}\hlkwb{const\ }\hlstd{Time\ hora\textunderscore sale\ }\hlsym{=\ {*}}\hlstd{t}\hlsym{;}\\
375 \hlline{136\ }\hlstd{}\hlstd{\ \ }\hlstd{}\hlkwb{const\ }\hlstd{Time\ hora\textunderscore llega\ }\hlsym{=\ {*}}\hlstd{t\ }\hlsym{+\ }\hlstd{distancia}\hlsym{{[}}\hlstd{v}\hlsym{{]};}\\
376 \hlline{137\ }\hlstd{}\hlstd{\ \ }\hlstd{}\hlkwa{if\ }\hlstd{}\hlsym{(}\hlstd{hora\textunderscore llega\ }\hlsym{$>$=\ }\hlstd{min\textunderscore hora\textunderscore llega}\hlsym{)\ }\hlstd{}\hlkwa{continue}\hlstd{}\hlsym{;}\\
377 \hlline{138\ }\hlstd{}\hlstd{\ \ }\hlstd{min\textunderscore hora\textunderscore llega\ }\hlsym{=\ }\hlstd{hora\textunderscore llega}\hlsym{;}\\
378 \hlline{139\ }\hlstd{}\hlstd{\ \ }\hlstd{salida}\hlsym{.}\hlstd{}\hlkwd{push\textunderscore front}\hlstd{}\hlsym{(}\hlstd{pair}\hlsym{$<$}\hlstd{Time}\hlsym{,\ }\hlstd{Time}\hlsym{$>$(}\hlstd{hora\textunderscore sale}\hlsym{,\ }\hlstd{distancia}\hlsym{{[}}\hlstd{v}\hlsym{{]}));}\\
379 \hlline{140\ }\hlstd{\ }\hlsym{\}}\\
380 \hlline{141\ }\hlstd{\ }\hlkwd{forall\ }\hlstd{}\hlsym{(}\hlstd{res}\hlsym{,\ }\hlstd{salida}\hlsym{)\ \{}\\
381 \hlline{142\ }\hlstd{}\hlstd{\ \ }\hlstd{}\hlkwd{printf}\hlstd{}\hlsym{(}\hlstd{}\hlstr{"\%.2i:\%.2i\ "}\hlstd{}\hlsym{,\ }\hlstd{res}\hlsym{{-}$>$}\hlstd{first\ }\hlsym{/\ }\hlstd{}\hlnum{60}\hlstd{}\hlsym{,\ }\hlstd{res}\hlsym{{-}$>$}\hlstd{first\ }\hlsym{\%\ }\hlstd{}\hlnum{60}\hlstd{}\hlsym{);}\\
382 \hlline{143\ }\hlstd{}\hlstd{\ \ }\hlstd{}\hlkwd{printf}\hlstd{}\hlsym{(}\hlstd{}\hlstr{"\%i:\%.2i}\hlesc{$\backslash$n}\hlstr{"}\hlstd{}\hlsym{,\ }\hlstd{res}\hlsym{{-}$>$}\hlstd{second\ }\hlsym{/\ }\hlstd{}\hlnum{60}\hlstd{}\hlsym{,\ }\hlstd{res}\hlsym{{-}$>$}\hlstd{second\ }\hlsym{\%\ }\hlstd{}\hlnum{60}\hlstd{}\hlsym{);}\\
383 \hlline{144\ }\hlstd{\ }\hlsym{\}}\\
384 \hlline{145\ }\hlstd{}\hlsym{\}}\\
385 \hlline{146\ }\hlstd{}\\
386 \hlline{147\ }\hldir{\#define\ add\textunderscore edge(g,\ v1,\ v2,\ c)\ ((g){[}(v1){]}.push\textunderscore back((Edge)\{dest:\ (v2),\ cost:\ (c)\}))}\\
387 \hlline{148\ }\hlstd{}\hlkwb{int\ }\hlstd{}\hlkwd{main}\hlstd{}\hlsym{()\ \{}\\
388 \hlline{149\ }\hlstd{\ }\hlkwb{int\ }\hlstd{ncases}\hlsym{;}\\
389 \hlline{150\ }\hlstd{\ cin\ }\hlsym{$>$$>$\ }\hlstd{ncases}\hlsym{;}\\
390 \hlline{151\ }\hlstd{\\
391 \hlline{152\ }\ }\hlkwd{forn\ }\hlstd{}\hlsym{(}\hlstd{c}\hlsym{,\ }\hlstd{ncases}\hlsym{)\ \{}\\
392 \hlline{153\ }\hlstd{}\hlstd{\ \ }\hlstd{Stations\ stations}\hlsym{;}\\
393 \hlline{154\ }\hlstd{}\hlstd{\ \ }\hlstd{Grafo\ graph}\hlsym{;}\\
394 \hlline{155\ }\hlstd{\\
395 \hlline{156\ }}\hlstd{\ \ }\hlstd{}\hlkwb{int\ }\hlstd{num\textunderscore trains}\hlsym{;}\\
396 \hlline{157\ }\hlstd{}\hlstd{\ \ }\hlstd{cin\ }\hlsym{$>$$>$\ }\hlstd{num\textunderscore trains}\hlsym{;}\\
397 \hlline{158\ }\hlstd{\\
398 \hlline{159\ }}\hlstd{\ \ }\hlstd{}\hlslc{//\ read\ trains\ and\ stations}\\
399 \hlline{160\ }\hlstd{}\hlstd{\ \ }\hlstd{}\hlkwd{forn\ }\hlstd{}\hlsym{(}\hlstd{t}\hlsym{,\ }\hlstd{num\textunderscore trains}\hlsym{)\ \{}\\
400 \hlline{161\ }\hlstd{}\hlstd{\ \ \ }\hlstd{}\hlkwb{int\ }\hlstd{num\textunderscore stations}\hlsym{;}\\
401 \hlline{162\ }\hlstd{}\hlstd{\ \ \ }\hlstd{cin\ }\hlsym{$>$$>$\ }\hlstd{num\textunderscore stations}\hlsym{;}\\
402 \hlline{163\ }\hlstd{\\
403 \hlline{164\ }}\hlstd{\ \ \ }\hlstd{Nodo\ prev}\hlsym{;}\\
404 \hlline{165\ }\hlstd{}\hlstd{\ \ \ }\hlstd{}\hlkwb{int\ }\hlstd{time\ }\hlsym{=\ }\hlstd{}\hlnum{0}\hlstd{}\hlsym{;}\\
405 \hlline{166\ }\hlstd{}\hlstd{\ \ \ }\hlstd{}\hlkwd{forn\ }\hlstd{}\hlsym{(}\hlstd{s}\hlsym{,\ }\hlstd{num\textunderscore stations}\hlsym{)\ \{}\\
406 \hlline{167\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{string\ time\textunderscore str}\hlsym{,\ }\hlstd{station\textunderscore name}\hlsym{;}\\
407 \hlline{168\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{cin\ }\hlsym{$>$$>$\ }\hlstd{time\textunderscore str\ }\hlsym{$>$$>$\ }\hlstd{station\textunderscore name}\hlsym{;}\\
408 \hlline{169\ }\hlstd{\\
409 \hlline{170\ }}\hlstd{\ \ \ \ }\hlstd{}\hlkwb{const\ }\hlstd{Time\ cost\ }\hlsym{=\ }\hlstd{}\hlkwd{time\textunderscore in\textunderscore minutes}\hlstd{}\hlsym{(}\hlstd{time\textunderscore str}\hlsym{);}\\
410 \hlline{171\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{time\ }\hlsym{=\ (}\hlstd{time\ }\hlsym{+\ }\hlstd{cost}\hlsym{)\ \%\ }\hlstd{DAY}\hlsym{;}\\
411 \hlline{172\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwb{const\ }\hlstd{Id\ station\textunderscore id\ }\hlsym{=\ }\hlstd{}\hlkwd{register\textunderscore station}\hlstd{}\hlsym{(}\hlstd{stations}\hlsym{,\ }\hlstd{station\textunderscore name}\hlsym{,\ }\hlstd{time}\hlsym{);}\\
412 \hlline{173\ }\hlstd{\\
413 \hlline{174\ }}\hlstd{\ \ \ \ }\hlstd{}\hlkwb{const\ }\hlstd{Nodo\ }\hlkwd{next}\hlstd{}\hlsym{(}\hlstd{station\textunderscore id}\hlsym{,\ }\hlstd{time}\hlsym{);}\\
414 \hlline{175\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwa{if\ }\hlstd{}\hlsym{(}\hlstd{s\ }\hlsym{!=\ }\hlstd{}\hlnum{0}\hlstd{}\hlsym{)\ \{}\\
415 \hlline{176\ }\hlstd{}\hlstd{\ \ \ \ \ }\hlstd{}\hlkwd{add\textunderscore edge}\hlstd{}\hlsym{(}\hlstd{graph}\hlsym{,\ }\hlstd{next}\hlsym{,\ }\hlstd{prev}\hlsym{,\ }\hlstd{cost}\hlsym{);}\\
416 \hlline{177\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlsym{\}}\\
417 \hlline{178\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{prev\ }\hlsym{=\ }\hlstd{next}\hlsym{;}\\
418 \hlline{179\ }\hlstd{}\hlstd{\ \ \ }\hlstd{}\hlsym{\}}\\
419 \hlline{180\ }\hlstd{}\hlstd{\ \ }\hlstd{}\hlsym{\}}\\
420 \hlline{181\ }\hlstd{\\
421 \hlline{182\ }}\hlstd{\ \ }\hlstd{}\hlslc{//\ complete\ the\ graph\ with\ the\ self\ edges\ for\ each\ station}\\
422 \hlline{183\ }\hlstd{}\hlstd{\ \ }\hlstd{}\hlkwd{forall\ }\hlstd{}\hlsym{(}\hlstd{station}\hlsym{,\ }\hlstd{stations}\hlsym{)\ \{}\\
423 \hlline{184\ }\hlstd{}\hlstd{\ \ \ }\hlstd{}\hlkwb{const\ }\hlstd{Id\ id\ }\hlsym{=\ }\hlstd{station}\hlsym{{-}$>$}\hlstd{\textunderscore info}\hlsym{.}\hlstd{\textunderscore id}\hlsym{;}\\
424 \hlline{185\ }\hlstd{}\hlstd{\ \ \ }\hlstd{}\hlkwb{const\ }\hlstd{set}\hlsym{$<$}\hlstd{Time}\hlsym{$>$\&\ }\hlstd{timeset\ }\hlsym{=\ }\hlstd{station}\hlsym{{-}$>$}\hlstd{\textunderscore info}\hlsym{.}\hlstd{\textunderscore timeset}\hlsym{;}\\
425 \hlline{186\ }\hlstd{}\hlstd{\ \ \ }\hlstd{}\hlkwa{if\ }\hlstd{}\hlsym{(}\hlstd{timeset}\hlsym{.}\hlstd{}\hlkwd{size}\hlstd{}\hlsym{()\ ==\ }\hlstd{}\hlnum{1}\hlstd{}\hlsym{)\ }\hlstd{}\hlkwa{continue}\hlstd{}\hlsym{;}\\
426 \hlline{187\ }\hlstd{}\hlstd{\ \ \ }\hlstd{}\hlkwd{forall\ }\hlstd{}\hlsym{(}\hlstd{t1}\hlsym{,\ }\hlstd{timeset}\hlsym{)\ \{}\\
427 \hlline{188\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{set}\hlsym{$<$}\hlstd{Time}\hlsym{$>$::}\hlstd{iterator\ t2\ }\hlsym{=\ }\hlstd{t1}\hlsym{;}\\
428 \hlline{189\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{t2}\hlsym{++;}\\
429 \hlline{190\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwb{const\ }\hlstd{Nodo\ }\hlkwd{v1}\hlstd{}\hlsym{(}\hlstd{id}\hlsym{,\ {*}}\hlstd{t1}\hlsym{);}\\
430 \hlline{191\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwb{const\ }\hlstd{Nodo\ }\hlkwd{v2}\hlstd{}\hlsym{(}\hlstd{id}\hlsym{,\ }\hlstd{t2\ }\hlsym{==\ }\hlstd{timeset}\hlsym{.}\hlstd{}\hlkwd{end}\hlstd{}\hlsym{()\ }\hlstd{?\ }\hlsym{{*}}\hlstd{timeset}\hlsym{.}\hlstd{}\hlkwd{begin}\hlstd{}\hlsym{()\ :\ {*}}\hlstd{t2}\hlsym{);}\\
431 \hlline{192\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{Time\ cost\ }\hlsym{=\ }\hlstd{v2}\hlsym{.}\hlstd{\textunderscore time\ }\hlsym{{-}\ }\hlstd{v1}\hlsym{.}\hlstd{\textunderscore time}\hlsym{;}\\
432 \hlline{193\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwa{if\ }\hlstd{}\hlsym{(}\hlstd{cost\ }\hlsym{$<$\ }\hlstd{}\hlnum{0}\hlstd{}\hlsym{)\ }\hlstd{cost\ }\hlsym{+=\ }\hlstd{DAY}\hlsym{;}\\
433 \hlline{194\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwd{add\textunderscore edge}\hlstd{}\hlsym{(}\hlstd{graph}\hlsym{,\ }\hlstd{v2}\hlsym{,\ }\hlstd{v1}\hlsym{,\ }\hlstd{cost}\hlsym{);}\\
434 \hlline{195\ }\hlstd{}\hlstd{\ \ \ }\hlstd{}\hlsym{\}}\\
435 \hlline{196\ }\hlstd{}\hlstd{\ \ }\hlstd{}\hlsym{\}}\\
436 \hlline{197\ }\hlstd{\\
437 \hlline{198\ }}\hlstd{\ \ }\hlstd{string\ origen}\hlsym{,\ }\hlstd{destino}\hlsym{;}\\
438 \hlline{199\ }\hlstd{}\hlstd{\ \ }\hlstd{cin\ }\hlsym{$>$$>$\ }\hlstd{origen\ }\hlsym{$>$$>$\ }\hlstd{destino}\hlsym{;}\\
439 \hlline{200\ }\hlstd{\\
440 \hlline{201\ }}\hlstd{\ \ }\hlstd{}\hlkwd{dijkstra}\hlstd{}\hlsym{(}\hlstd{stations}\hlsym{,\ }\hlstd{graph}\hlsym{,\ }\hlstd{origen}\hlsym{,\ }\hlstd{destino}\hlsym{);}\\
441 \hlline{202\ }\hlstd{}\hlstd{\ \ }\hlstd{}\hlkwa{if\ }\hlstd{}\hlsym{(}\hlstd{c\ }\hlsym{!=\ }\hlstd{ncases\ }\hlsym{{-}\ }\hlstd{}\hlnum{1}\hlstd{}\hlsym{)\ \{}\\
442 \hlline{203\ }\hlstd{}\hlstd{\ \ \ }\hlstd{cout\ }\hlsym{$<$$<$\ }\hlstd{endl}\hlsym{;}\\
443 \hlline{204\ }\hlstd{}\hlstd{\ \ }\hlstd{}\hlsym{\}}\\
444 \hlline{205\ }\hlstd{\ }\hlsym{\}}\\
445 \hlline{206\ }\hlstd{\ }\hlkwa{return\ }\hlstd{}\hlnum{0}\hlstd{}\hlsym{;}\\
446 \hlline{207\ }\hlstd{}\hlsym{\}}\\
447 \hlline{208\ }\hlstd{}\\
448 \mbox{}
449 \normalfont
450 \shorthandon{"}